﻿using System;
using System.Web;
using System.Web.Routing;

namespace BlueSky.Web.Routing
{
    public class RangeConstraint : IRouteConstraint
    {
        protected int _min;

        protected int _max;

        public RangeConstraint(int min, int max)
        {
            _min = min;
            _max = max;
        }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values[parameterName] != null)
            {
                string sValue = values[parameterName].ToString();
                if(!string.IsNullOrEmpty(sValue))
                {
                    int iValue;
                    if (int.TryParse(sValue, out iValue))
                    {
                        if (_min <= iValue && iValue <= _max)
                            return true;
                    }
                }
            }
            return false;
        }
    }
}
